﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_UpdateByUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 15)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            TarreOA.TarreOAModels.User user=(User)Session["user"];
            UserInfo userInfo = UserInfoManager.FindByLoginName(user.LoginName);
            txtLoginName.Text = user.LoginName;
            txtUserName.Text = user.UserName;
            txtAddress.Text = userInfo.Address;
            txtEmail.Text = userInfo.Email;
            txtFinishShool.Text = userInfo.FinishShool;
            txtFinishShoolTime.Text = userInfo.FinishShoolTime.ToShortDateString();
            txtIDCard.Text = userInfo.IDCard;
            txtTelephone.Text = userInfo.Telephone;
            ddlAcademicCertificate.SelectedValue = userInfo.AcademicCertificate.ToString();
            ddlNation.SelectedValue = userInfo.Nation.NationId.ToString();
            ddlSex.SelectedValue = userInfo.Sex.ToString();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        TarreOA.TarreOAModels.User user = new User();
        UserInfo userInfo = new UserInfo();
        user.LoginName = txtLoginName.Text;
        user.UserName = txtUserName.Text;
        userInfo.Address = txtAddress.Text;
        userInfo.Email = txtEmail.Text;
        userInfo.FinishShool = txtFinishShool.Text;
        if (txtFinishShoolTime.Text!=null && txtFinishShoolTime.Text != "")
        {
            userInfo.FinishShoolTime = Convert.ToDateTime(txtFinishShoolTime.Text);
        }
        userInfo.IDCard = txtIDCard.Text;
        userInfo.Telephone = txtTelephone.Text;
        userInfo.Mob = mob.Text;
        userInfo.AcademicCertificate = Convert.ToInt32(ddlAcademicCertificate.SelectedValue);
        Nation nation = new Nation();
        nation.NationId = Convert.ToInt32(ddlNation.SelectedValue);
        userInfo.Nation = nation;
        userInfo.Sex = Convert.ToBoolean(ddlSex.SelectedValue);
        if (UserInfoManager.UpdateUser(user, userInfo))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功！')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改失败！请重试！')", true);
        }
    }
}
